package com.monster.servlet;

import com.alibaba.fastjson.JSONArray;
import com.monster.pojo.Album;
import com.monster.pojo.Singer;
import com.monster.service.AlbumService;
import com.monster.service.ListServiceImpl;
import com.monster.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/AlbumPageServlet")
public class AlbumPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        switch (opr) {
            case "showAll":
                showAll(request,response);
                break;
            case "searchByArea":
                searchByArea(request,response);
                break;
            case "recommand":
                recommand(request,response);
                break;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    //首页进来查全部
    private void showAll(HttpServletRequest request, HttpServletResponse response) {
        AlbumService albumService = new ListServiceImpl();
        HttpSession session = request.getSession();//创建
        ArrayList<Album> alList = new ArrayList<>();
        String no = request.getParameter("page");
        int page = Integer.parseInt(no);
        alList = albumService.getAllAlbums(page);
        int totalPage = PageUtil.getTotalPage(30);
        if (alList != null) {
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("alList", alList);
            try {
                request.getRequestDispatcher("album.jsp").forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //专辑页面点击地区查询
    private void searchByArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AlbumService albumService = new ListServiceImpl();
        PrintWriter out = response.getWriter();
        ArrayList<Album> alList = new ArrayList<>();
        ArrayList<Album> List = new ArrayList<>();
        String no = request.getParameter("page");
        int page = Integer.parseInt(no);
        String language = request.getParameter("album_language");
        String genre = request.getParameter("album_area_choice");
        String orderType = request.getParameter("orderType");
        alList = albumService.getAllAlbums(language,genre,orderType,page);
        int totalPage = PageUtil.getTotalPage(30);
        Album album = null;
        Singer singer = null;
        for(Album al:alList){
            album = new Album();
            singer = new Singer();
            album.setAlbumId(al.getAlbumId());
            album.setAlbumName(al.getAlbumName());
            album.setAlbumPoster(al.getAlbumPoster());
            album.setTotalPage(totalPage);
            singer.setSingerName(al.getSinger().getSingerName());
            singer.setSingerId(al.getSinger().getSingerId());
            singer.setPoster(al.getSinger().getPoster());
            album.setSinger(singer);
            List.add(album);
        }
        out.print(JSONArray.toJSON(List));
        out.flush();
        out.close();
    }

    private void recommand(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        AlbumService albumService = new ListServiceImpl();
        HttpSession session = request.getSession();//创建
        ArrayList<Album> alList = new ArrayList<>();
        ArrayList<Album> List = new ArrayList<>();
        String no = request.getParameter("page");
        int page = Integer.parseInt(no);
        alList = albumService.getAllAlbums(page);
        int totalPage = PageUtil.getTotalPage(30);
        Album album = null;
        Singer singer = null;
        for(Album al:alList){
            album = new Album();
            singer = new Singer();
            album.setAlbumId(al.getAlbumId());
            album.setAlbumName(al.getAlbumName());
            album.setAlbumPoster(al.getAlbumPoster());
            album.setTotalPage(totalPage);
            singer.setSingerName(al.getSinger().getSingerName());
            singer.setSingerId(al.getSinger().getSingerId());
            singer.setPoster(al.getSinger().getPoster());
            album.setSinger(singer);
            List.add(album);
        }
        out.print(JSONArray.toJSON(List));
        out.flush();
        out.close();
    }
}